package com.xlhj.boot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: caijliu
 * @Date: 04/09/2021 14:39
 * @Description 延迟队列(消息延迟)配置类
 * @Vsersion: 0.0.1
 */
@Configuration
public class TtlMsgConfig {

    private static final String TTL_MSG_QUEUE_EXCHANGE = "ttl-msg-queue-exchange";
    private static final String DEAD_LETTER_EXCHANGE = "dead-letter-exchange";
    private static final String TTL_MSG_QUEUE = "ttl-msg-queue";
    private static final String DEAD_LETTER_ROUT_KEY = "dead-letter-routing-key";
    private static final String TTL_MSG_QUEUE_ROUT_KEY = "ttl_msg_queue-routing-key";

    @Bean(name = "ttlMsgQueueExchange")
    public DirectExchange ttlMsgQueueExchange() {
        return new DirectExchange(TTL_MSG_QUEUE_EXCHANGE);
    }

    @Bean(name = "ttlMsgQueue")
    public Queue ttlMsgQueue() {
        Map<String, Object> map = new HashMap<>(3);
        map.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", DEAD_LETTER_ROUT_KEY);
        return QueueBuilder.durable(TTL_MSG_QUEUE).withArguments(map).build();
    }

    @Bean
    public Binding ttlMsgQueueBinding(@Qualifier("ttlMsgQueue") Queue queue, @Qualifier("ttlMsgQueueExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TTL_MSG_QUEUE_ROUT_KEY);
    }
}
